﻿using System;
using UnityEngine;

// Token: 0x0200013E RID: 318
public abstract class Motor : MonoBehaviour
{
	// Token: 0x060005CD RID: 1485 RVA: 0x0002E044 File Offset: 0x0002C244
	public virtual void Start()
	{
		this.vp = (VehicleParent)F.GetTopmostParentComponent<VehicleParent>(base.transform);
		this.snd = base.GetComponent<AudioSource>();
		if (this.snd)
		{
			this.snd.pitch = this.minPitch;
		}
		if (this.boostLoopSnd)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.boostLoopSnd.gameObject, this.boostLoopSnd.transform.position, this.boostLoopSnd.transform.rotation);
			this.boostSnd = gameObject.GetComponent<AudioSource>();
			this.boostSnd.transform.parent = this.boostLoopSnd.transform;
			this.boostSnd.transform.localPosition = Vector3.zero;
			this.boostSnd.transform.localRotation = Quaternion.identity;
			this.boostSnd.loop = false;
		}
		if (this.smoke)
		{
			this.initialSmokeEmission = this.smoke.emission.rateOverTime.constantMax;
		}
	}

	// Token: 0x060005CE RID: 1486 RVA: 0x0002E164 File Offset: 0x0002C364
	public virtual void FixedUpdate()
	{
		this.health = Mathf.Clamp01(this.health);
		this.boost = Mathf.Clamp((!this.boosting) ? this.boost : (this.boost - this.boostBurnRate * Time.timeScale * 0.05f * TimeMaster.inverseFixedTimeFactor), 0f, this.maxBoost);
		this.boostPrev = this.boosting;
		if (this.canBoost && this.ignition && this.health > 0f && !this.vp.crashing && this.boost > 0f && ((!this.vp.hover) ? (this.vp.accelInput > 0f || this.vp.localVelocity.z > 1f) : (this.vp.accelInput != 0f || Mathf.Abs(this.vp.localVelocity.z) > 1f)))
		{
			if (((this.boostReleased && !this.boosting) || this.boosting) && this.vp.boostButton)
			{
				this.boosting = true;
				this.boostReleased = false;
			}
			else
			{
				this.boosting = false;
			}
		}
		else
		{
			this.boosting = false;
		}
		if (!this.vp.boostButton)
		{
			this.boostReleased = true;
		}
		if (this.boostLoopSnd && this.boostSnd)
		{
			if (this.boosting && !this.boostLoopSnd.isPlaying)
			{
				this.boostLoopSnd.Play();
			}
			else if (!this.boosting && this.boostLoopSnd.isPlaying)
			{
				this.boostLoopSnd.Stop();
			}
			if (this.boosting && !this.boostPrev)
			{
				this.boostSnd.clip = this.boostStart;
				this.boostSnd.Play();
			}
			else if (!this.boosting && this.boostPrev)
			{
				this.boostSnd.clip = this.boostEnd;
				this.boostSnd.Play();
			}
		}
	}

	// Token: 0x060005CF RID: 1487 RVA: 0x0002E3E4 File Offset: 0x0002C5E4
	public virtual void Update()
	{
		if (!this.ignition)
		{
			this.targetPitch = 0f;
		}
		if (this.snd)
		{
			if (this.ignition && this.health > 0f)
			{
				this.snd.enabled = true;
				this.snd.pitch = Mathf.Lerp(this.snd.pitch, Mathf.Lerp(this.minPitch, this.maxPitch, this.targetPitch), 20f * Time.deltaTime) + Mathf.Sin(Time.time * 200f * (1f - this.health)) * (1f - this.health) * 0.1f * this.damagePitchWiggle;
				this.snd.volume = Mathf.Lerp(this.snd.volume, 0.3f + this.targetPitch * 0.7f, 20f * Time.deltaTime);
			}
			else
			{
				this.snd.enabled = false;
			}
		}
		if (this.boostParticles.Length > 0)
		{
			foreach (ParticleSystem particleSystem in this.boostParticles)
			{
				if (this.boosting && particleSystem.isStopped)
				{
					particleSystem.Play();
				}
				else if (!this.boosting && particleSystem.isPlaying)
				{
					particleSystem.Stop();
				}
			}
		}
		if (this.smoke)
		{
			this.smoke.emission.rateOverTime = new ParticleSystem.MinMaxCurve((this.health >= 0.7f) ? 0f : (this.initialSmokeEmission * (1f - this.health)));
		}
	}

	// Token: 0x04000872 RID: 2162
	protected VehicleParent vp;

	// Token: 0x04000873 RID: 2163
	public bool ignition;

	// Token: 0x04000874 RID: 2164
	public float power = 1f;

	// Token: 0x04000875 RID: 2165
	[Tooltip("Throttle curve, x-axis = input, y-axis = output")]
	public AnimationCurve inputCurve = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f);

	// Token: 0x04000876 RID: 2166
	protected float actualInput;

	// Token: 0x04000877 RID: 2167
	protected AudioSource snd;

	// Token: 0x04000878 RID: 2168
	[Header("Engine Audio")]
	public float minPitch;

	// Token: 0x04000879 RID: 2169
	public float maxPitch;

	// Token: 0x0400087A RID: 2170
	[NonSerialized]
	public float targetPitch;

	// Token: 0x0400087B RID: 2171
	protected float pitchFactor;

	// Token: 0x0400087C RID: 2172
	protected float airPitch;

	// Token: 0x0400087D RID: 2173
	[Header("Nitrous Boost")]
	public bool canBoost = true;

	// Token: 0x0400087E RID: 2174
	[NonSerialized]
	public bool boosting;

	// Token: 0x0400087F RID: 2175
	public float boost = 1f;

	// Token: 0x04000880 RID: 2176
	private bool boostReleased;

	// Token: 0x04000881 RID: 2177
	private bool boostPrev;

	// Token: 0x04000882 RID: 2178
	[Tooltip("X-axis = local z-velocity, y-axis = power")]
	public AnimationCurve boostPowerCurve = AnimationCurve.EaseInOut(0f, 0.1f, 50f, 0.2f);

	// Token: 0x04000883 RID: 2179
	public float maxBoost = 1f;

	// Token: 0x04000884 RID: 2180
	public float boostBurnRate = 0.01f;

	// Token: 0x04000885 RID: 2181
	public AudioSource boostLoopSnd;

	// Token: 0x04000886 RID: 2182
	private AudioSource boostSnd;

	// Token: 0x04000887 RID: 2183
	public AudioClip boostStart;

	// Token: 0x04000888 RID: 2184
	public AudioClip boostEnd;

	// Token: 0x04000889 RID: 2185
	public ParticleSystem[] boostParticles;

	// Token: 0x0400088A RID: 2186
	[Range(0f, 1f)]
	[Header("Damage")]
	public float strength = 1f;

	// Token: 0x0400088B RID: 2187
	[NonSerialized]
	public float health = 1f;

	// Token: 0x0400088C RID: 2188
	public float damagePitchWiggle;

	// Token: 0x0400088D RID: 2189
	public ParticleSystem smoke;

	// Token: 0x0400088E RID: 2190
	private float initialSmokeEmission;
}
